package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.FilterIncomeListDao;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.FilterIncomeList;
import com.sc.nft.entity.UserCreatorInfo;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.FilterListVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.util.CheckAccountUtil;
import jnr.ffi.annotations.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.FilterIncomeListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务接口实现
 *
 * @author Sun
 * @since 2022-10-18 11:00:01
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class FilterIncomeListServiceImpl implements FilterIncomeListService {
    private final FilterIncomeListDao filterIncomeListDao;
    private final UserInfoDao userInfoDao;
    private final UserCreatorInfoDao userCreatorInfoDao;

    @Override
    public Boolean getIsTeamIncomeByUserId(Long userId) {
        FilterIncomeList filterIncomeList = filterIncomeListDao.getIsTeamIncomeByUserId(userId);
        return ObjectUtil.isNull(filterIncomeList);
    }

    @Override
    public Page<FilterListVO> getPageFilterList(Long daoId, Integer pageNo, Integer pageSize,String userTel) {
        return filterIncomeListDao.getPageFilterList(daoId, pageNo,pageSize,userTel);
    }

    @Override
    public void addByUserTel(String userTel,Long daoId) {
        UserInfo userInfo = userInfoDao.getOneByUserTel(userTel);
        if(ObjectUtil.isNull(userInfo)){
            throw new GlobalRunTimeException(ErrorCode.THE_USER_DOES_NOT_EXIST);
        }
        UserCreatorInfo dao = userCreatorInfoDao.getById(daoId);
        if(ObjectUtil.isNull(dao)){
            throw new GlobalRunTimeException("DAO不存在");
        }

        FilterIncomeList filterIncomeList = filterIncomeListDao.getIsTeamIncomeByUserId(userInfo.getId());
        if(ObjectUtil.isNotNull(filterIncomeList)){
            throw new GlobalRunTimeException(ErrorCode.ACCOUNT_EXISTS);
        }
        FilterIncomeList filterIncomeListInsert = new FilterIncomeList();//
        filterIncomeListInsert.setUserId(userInfo.getId());
        filterIncomeListInsert.setDaoId(daoId);
        filterIncomeListInsert.insert();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editByUserTel(Long id, String userTel, Long daoId) {
        UserInfo userInfo = userInfoDao.getOneByUserTel(userTel);
        if(ObjectUtil.isNull(userInfo)){
            throw new GlobalRunTimeException(ErrorCode.THE_USER_DOES_NOT_EXIST);
        }
        UserCreatorInfo dao = userCreatorInfoDao.getById(daoId);
        if(ObjectUtil.isNull(dao)){
            throw new GlobalRunTimeException("DAO不存在");
        }

        FilterIncomeList filterIncomeList = filterIncomeListDao.getIsTeamIncomeByUserId(userInfo.getId());
        if(ObjectUtil.isNull(filterIncomeList)){
            throw new GlobalRunTimeException(ErrorCode.ACCOUNT_EXISTS);
        }
        // 如果已经分配了dao的话就不允许在分配了
        if (filterIncomeList.getDaoId()>0L) {
            throw new GlobalRunTimeException(ErrorCode.MARKET_VALUE_ACCOUNT_HAS_BEEN_ALLOCATED);
        }
        FilterIncomeList filterIncomeListInsert = new FilterIncomeList();
        filterIncomeListInsert.setId(id);
        filterIncomeListInsert.setUserId(userInfo.getId());
        filterIncomeListInsert.setDaoId(daoId);
        filterIncomeListInsert.updateById();
    }

    @Override
    public void deleteById(Long id) {
        FilterIncomeList filterIncomeList = filterIncomeListDao.getById(id);
        if(ObjectUtil.isNull(filterIncomeList)){
            throw new GlobalRunTimeException(ErrorCode.THE_USER_DOES_NOT_EXIST);
        }
        filterIncomeListDao.updateIsDeleteById(id);

    }

    @Override
    public void updateFilterUserPwd(String userTel, String pwd) {
        UserInfo userInfo = userInfoDao.getOneByUserTel(userTel);
        Assert.notNull(userInfo,()-> new GlobalRunTimeException("用户不存在"));
        Assert.notNull( filterIncomeListDao.getByUserId(userInfo.getId()),()-> new GlobalRunTimeException("市值不存在"));

        String newPwd = CheckAccountUtil.pwdToMd5(pwd);
        if (!userInfoDao.updatePwdById(userInfo.getId(), newPwd)) {
            throw new GlobalRunTimeException("修改失败");
        }

    }
}